<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.lable.DICbaoZhuangDanWeiChaKan"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
<!--[if lt IE 9]>
    <script src="lib/js/html5shiv.min.js"></script>
    <script src="lib/js/respond.min.js"></script>
<![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1><spring:message code="message.lable.DICbaoZhuangDanWeiChaKan"/></h1>
        <ol class="breadcrumb">
             <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.lable.DicManage"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.lable.packunitDic"/></a></li>
        </ol>
    </section>
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
            <form action="${ctx}/dicPackunit/exportExcel.html" id="exportExcel" method="post">
            <input id="fileName" name="fileName" type="hidden" value="<spring:message code="message.lable.packunitxls"/>" />
                    <div class="input">
            		<div class="textalign1"><spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="packunitNameSearch" name="packunitName" placeholder="<spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/>" />
                    </div>
                    <div class="textalign1"><spring:message code="message.listForm.baoZhuangDanWeiBianMa"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="packunitCodeSearch" name="packunitCode" placeholder="<spring:message code="message.listForm.baoZhuangDanWeiBianMa"/>" />
                    </div>
                    
                    <div class="textalign1"><spring:message code="message.lable.status"/>：</div>
	                  <div class="boxinput">
                        <select id="status" name="status" class="textinput">
                            	<option value="" ><spring:message code="message.select.option"/></option>
	                            <option value="1" ><spring:message code="message.button.start"/></option>
	                            <option value="0" ><spring:message code="message.button.stop"/></option>
                            </select>
                    </div>
                    <div class="textalign" >
                    <button type="button" onclick="likeQuery();" class="btn btn-success btn-sm" id="selectDicCompType"> <spring:message code="message.button.seachSpacing"/></button>
                </div>
                </div>
                </form>
            </div>
        </div>
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        <div class=" box box-warning bottongroup">
        
            <ms:btn url="/dicPackunit/add.html"><label id="addNew" class="btn btn-default btn-sm"><spring:message code="message.button.addSpacing"/></label></ms:btn>
            <ms:btn url="/dicPackunit/enable.html"><label id="allStart" class="btn btn-default btn-sm"><spring:message code="message.button.startSpacing"/></label></ms:btn>
            <ms:btn url="/dicPackunit/disable.html"><label id="allDelete" class="btn btn-default btn-sm"><spring:message code="message.button.stopSpacing"/></label></ms:btn>
            <label onclick="exportExcel();" class="btn btn-default btn-sm"><spring:message code="message.lable.daoChu"/></label>
        </div>
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel"><spring:message code="message.lable.DICbaoZhuangDanWeiXinZeng"/></h5>
                    </div>
                    <div class="modal-body">
                     <form>
                     <div class="form-group form-group-xs">
                        <label for="packunitName" class=" control-label"><span style="color:red">*&nbsp;</span><spring:message code="message.listForm.baoZhuangDanWeiBianMa"/>：</label>
                        <div class="">
                            <input type="text" class="form-control" id="packunitCode" placeholder="<spring:message code="message.listForm.baoZhuangDanWeiBianMa"/>" maxlength="20" >
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="packunitName" class=" control-label"><span style="color:red">*&nbsp;</span><spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/>：</label>
                        <div class="">
                            <input type="text" class="form-control" id="packunitName" placeholder="<spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/>" maxlength="100" >
                        </div>
                    </div>
                    </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" onclick="save();" class="btn btn-primary btn-sm"><spring:message code="message.button.saveSpacing"/></button>
                        <button type="button" class="btn btn-danger btn-sm" data-dismiss="modal"><spring:message code="message.button.closeSpacing"/></button>
                    </div>
                </div>
            </div>
        </div>
    </section>
   <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
  //绑定回车
    $('#packunitCodeSearch,#packunitNameSearch,#status').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    	/* 列表展示 */
        $(function () {
        	$("#packunitNameSearch").focus();//加载页面光标在第一个查询框上
        	
        	
            $("#gridlist").jqGrid({
	                url: "getDicDrugspecList.html",
	                mtype: "POST",
	                datatype: "json",
	                autowidth: true,
	                height: 270,
	                colNames: ['',
	                           '<spring:message code="message.jqGrid.cz"/>',
	                           '<spring:message code="message.listForm.baoZhuangDanWeiBianMa"/>', 
	                           '<spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/>',
	                           '<spring:message code="message.lable.status"/>'],
	                colModel: [
	                    { name: 'packunitId', index: 'packunitId', hidden:true, width: 50, align:"center", sortable: false},
	                    { name: 'operate', width: 20,sortable: false, align:"center",
                            formatter : function(val, opts, rowdata)
                            {
                                var str =  "<ms:btn url='/dicPackunit/edit.html'><a href=\"javascript:edit('"+rowdata.packunitName+"','"+rowdata.packunitCode+ "','"+rowdata.packunitId+"','"+rowdata.status+"');\"  style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat 0px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.bianJi"/>'></a></ms:btn>";
                                str += "<ms:btn url='/dicPackunitLog/log.html'><a href='${ctx}/dicPackunitLog/log.html?packunitId="+rowdata.packunitId+"&packunitCode="+rowdata.packunitCode+"&packunitName="+rowdata.packunitName+"'  style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.weiHuJiLu"/>'></a></ms:btn>";
                                return str;
                                }
	                    }  ,
	                    { name: 'packunitCode', index: 'packunitCode', width: 100, align:"center", sortable: false},
	                    { name: 'packunitName', index: 'packunitName', width: 100, align:"center", sortable: false },
	                    { name: 'status', index: 'status', width: 30 , align: 'center', sortable: false,
	                    	cellattr:function (rowId, val, rawObject, cm) {
                                if(val == '停用'){
                                    return 'style="color:red"';
                                }
                            },
	                    	formatter : function(val, opts, rowdata) {
	                    		var strSta = "";
	                    		if(rowdata.status == 1){
	                    			strSta += "<spring:message code="message.button.start"/>";
	                    		}else{
	                    			strSta += "<spring:message code="message.button.stop"/>";
	                    		}
	                    		return strSta;
	                    	}
	                    }
	                     
                ],
                rowNum: 20,
                rowList: [10, 20, 50,100],
                rownumbers: true,
                pager: "#gridpage",
                viewrecords: true,
                multiselect: true, 
                caption: "<spring:message code="message.list.packunitDiclist"/>",
                //序号宽度自动变化
                gridComplete:function(){
                	//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                	//数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
                },
                jsonReader: {
                    repeatitems: false,
                    id: "packunitId"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
        });
        
        
        /* 查询功能 */
        function likeQuery(page){
        	if (page == ""){
        		page = 1;
        	}
            var packunitCode  = $("#packunitCodeSearch").val();
            var packunitName = $("#packunitNameSearch").val();
            var status = $("#status").val();
            $("#gridlist").jqGrid('setGridParam', { 
                postData:{
                    "packunitCode":packunitCode,
                    "packunitName":packunitName,
                    "status":status
                },
                page: page
        }).trigger("reloadGrid")
        }
        
        
        /* 启用停用（包含批量启用停用）功能  */
        //启用按钮
        $('#allStart').click(function(){
            batchChangeStatus(1);
        })
        //停用按钮
        $('#allDelete').click(function(){
            batchChangeStatus(0);
        })
        //启用停用功能实现
        function batchChangeStatus(status) {
        	var msg = "";
        	var url = ""; 
        	if (status == 1){
        		msg = "<spring:message code="message.button.start"/>";
        		url = "${ctx}/dicPackunit/disable.html";
        	} else if (status == 0){
        		msg = "<spring:message code="message.button.stop"/>";
        		url = "${ctx}/dicPackunit/enable.html"; 
        	}
			var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
			if (rowIds.length <= 0) {
				$.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"<spring:message code="message.listForm.baoZhuangDanWei"/>", "warn");
				return;
			}
			
			var str = "";
        	var names = "";
        		for (var i = 0;i < rowIds.length; i++){
        			var rowdata = $("#gridlist").jqGrid('getRowData', rowIds[i]);
            		if (status == 1){
            			if (rowdata.status == "<spring:message code="message.button.stop"/>"){
            				names += rowdata.packunitName+",";
            				str += rowIds[i]+",";
            			}
            		} else {
            			if (rowdata.status == "<spring:message code="message.button.start"/>"){
            				names += rowdata.packunitName+",";
            				str += rowIds[i]+",";
            			}
            		}
            	}
        		if (names != ""){
        			names = names.substring(0,names.length-1);
        		} else {
        			$.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"<spring:message code="message.lable.OKData"/>", "warn");
            		return;
        		}
			
        		  $.HN.message.confirm('<spring:message code="message.lable.OKYES"/>'+msg+'<spring:message code="message.lable.FlowerDataYESorNo"/><br/>'+names, '', '').on(function (e) {
	            if (e) {
	            	if (str.length > 0){
                		str = str.substring(0,str.length-1);
                	}
			var list = [];
			for (var i=0; i<rowIds.length; i++) {
				rowId = rowIds[i];
				list.push({"packunitId":rowId, "status":status});
			}
			$.post(url, {
				"array" : json.jsonToString(list)
			}, function(result) {
				if (result.success) {
					$.alert("<spring:message code="message.HN.alert.chengGong"/>"+msg+result.conditions.count+"<spring:message code="message.HN.alert.tiaoShuJu"/>", "success");
					likeQuery();
					$("#gridlist").trigger("reloadGrid");
				} else {
					$.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
				}
			}, "json");
	            }
			});
		}
        
        
        /* 新增弹窗 */
        $('#addNew').click(function () {
            $('.modal-title').html('<spring:message code="message.lable.DICbaoZhuangDanWeiXinZeng"/>');
            id="";
            status="";
            $('#packunitName').val("");
            $('#packunitCode').val("");
            $('#exampleModal').modal({
                keyboard: true
            });
        });
        
        /* 编辑弹窗 */
        function edit(packunitName,packunitCode,packunitId,status){
            id = packunitId;
            status=status;
            $('.modal-title').html('<spring:message code="message.lable.DICbaoZhuangDanWeiBianJi"/>');
            $('#packunitName').val(packunitName);
            $('#packunitCode').val(packunitCode);
            $('#exampleModal').modal({
                 keyboard: true
             });
            }
    
        /* 保存新增编辑 */
        function save(){
        	 if (!validate()) {
	    			return;
	    	}
        	 var packunitName = $("#packunitName").val();
        	 var packunitCode = $("#packunitCode").val();
      	     $.HN.message.confirm('<spring:message code="message.HN.alert.OkSaveYESorNO"/>', '', '').on(function (e) {
                if (e) {
                	var url = "${ctx}/dicPackunit/add.html";
                    if(id !=null && id != ""){      
                    	url = "${ctx}/dicPackunit/edit.html";      
                    	}
                	$.ajax({
               		   type: "POST",
               		   url: url,
               		   data: {
               			   packunitCode:packunitCode,
               			   packunitName:packunitName,
               			   packunitId:id,
               			   status:status},
               		   dataType:"json",
               		   success: function(result){
               			   if(result.success){
               				$.alert("<spring:message code="message.HN.alert.success"/>", "success");
               				$("#gridlist").trigger("reloadGrid");
               				$('#exampleModal').modal('hide');
               			   }else{
               				$.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
                			   }
                		   }
                	});
                }
            });
        }
       

        
        /* 新增编辑输入框校验 */
        function validate() {
        	var packunitName = $.trim($("#packunitName").val());
       	 	var packunitCode = $.trim($("#packunitCode").val());
       	 	
	       	if(packunitCode==""){
			//	$("#packunitCode").trigger("focus");
                $.alert("<spring:message code="message.listForm.baoZhuangDanWeiBianMa"/><spring:message code="message.yanZheng.buNengWeiKong"/>", "warn");
	 			return false;
			}
	       	if(!/^[a-zA-Z0-9_]+$/.test(packunitCode)){
             //   $("#packunitCode").trigger("focus");
                $.alert("<spring:message code="message.listForm.baoZhuangDanWeiBianMa"/><spring:message code="message.yanZheng.zhiNengYouZiMuShuZiZuCheng"/>", "warn");
                return false;
	            }
			if(packunitCode.length>20){
			//	$("#packunitCode").trigger("focus");
                $.alert("<spring:message code="message.listForm.baoZhuangDanWeiBianMa"/><spring:message code="message.yanZheng.changDuGuoChang"/>", "warn");
    			return false;
			}
			
			
			if(packunitName==""){
			//	$("#packunitName").trigger("focus");
                $.alert("<spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/><spring:message code="message.yanZheng.buNengWeiKong"/>", "warn");
	 			return false;
			}
			if(packunitName.length>100){
			//	$("#packunitName").trigger("focus");
                $.alert("<spring:message code="message.listForm.baoZhuangDanWeiMingCheng"/><spring:message code="message.yanZheng.changDuGuoChang"/>", "warn");
    			return false;
			}
    		return true;
    	}
        
        function exportExcel(){
     		$.HN.message.confirm('<spring:message code="message.lable.OKExport"/>&nbsp;<spring:message code="message.lable.packunitxls"/>&nbsp;<spring:message code="message.lable.ma"/>', '', '').on(function (e)  {
	               if (e) {
	            	  $("#exportExcel").submit();
			
	               }
          	 }); 
          	
     	}
      
    </script>
</body>
</html>